library(tidyverse)
library(lubridate)

news <- read_csv("../results/merged_news.csv") %>%
  filter(party != "ind")

subjects <- news %>% group_by(party, caseid) %>%
  summarise(
    avg_b_align = mean(b_align, na.rm = TRUE),
    total_visits = n(),
    portal_frac = mean(portal)) %>% 
  ungroup()

subjects <- subjects %>% group_by(party) %>%
  summarize(m_align = median(avg_b_align, na.rm = TRUE)) %>% 
  right_join(subjects, by = "party") %>% 
  mutate(party_bin = case_when(
    party == "dem" & avg_b_align <= m_align ~ "far_dem",
    party == "dem" & avg_b_align > m_align ~ "mod_dem",
    party == "rep" & avg_b_align > m_align ~ "far_rep",
    party == "rep" & avg_b_align <= m_align ~ "mod_rep"
  ))

create_panel <- function(event, event_date) {
  event_date <- ymd(event_date)
  out_filename <- paste0("../results/", event, "_panel.csv")

  panel <- news %>% group_by(caseid, date) %>% 
    summarise(num_urls = n(),
      num_portals = sum(domain_cat == "portal"),
      num_online = sum(domain_cat == "online"),
      num_legacy = sum(domain_cat == "legacy"),
      num_polling = sum(domain_cat == "polling"),
      date_b_align = mean(b_align, na.rm = TRUE)) %>% 
    ungroup() %>% 
    complete(caseid, date,
      fill = list(num_urls = 0, num_portals = 0,
        num_online = 0, num_legacy = 0, num_polling = 0,
        date_b_align = NA)) %>% 
    left_join(subjects, by = "caseid")

  panel <- panel %>% group_by(caseid) %>%
    summarise(last_date = max(date[num_urls > 0])) %>% 
    left_join(panel, ., by = "caseid") %>% 
    filter(!(last_date < event_date)) %>% 
    mutate(event_date = event_date,
      days_after = date - event_date) %>% 
    ungroup()

  panel %>% write_csv(out_filename)
}

create_panel("PLACEBO1", "2016-09-19") # original placebo
create_panel("PLACEBO2", "2016-09-05")
create_panel("PLACEBO3", "2016-10-15")
create_panel("AH", "2016-10-07")
create_panel("CL", "2016-10-28")
create_panel("FALL", "2016-09-11")
create_panel("SEP_DEBATE", "2016-09-26")

create_panel("DESC_pre", "2016-11-05") 